package cn.eyesea.cc.util;

import cn.eyesea.cc.Main;
import cn.eyesea.cc.config.DomainConfig;
import cn.eyesea.cc.config.GlobalConfig;
import cn.eyesea.cc.entity.TypeMapper;
import cn.eyesea.cc.tool.GlobalTool;
import cn.eyesea.cc.tool.TimeUtils;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;

import java.io.*;
import java.util.*;

/**
 * TODO
 *
 * @author wanghuilong
 * @version 1.0
 * @date 2021/7/12 3:34 下午
 */
@Slf4j
public class ConfigUtil {


    /**
     * 获取数据库类型映射
     *
     * @param globalConfig
     */
    public static void configType(GlobalConfig globalConfig) {
        List<TypeMapper> typeMapperList = new ArrayList<>();
        typeMapperList.add(new TypeMapper("varchar(\\(\\d+\\))?", "java.lang.String"));
        typeMapperList.add(new TypeMapper("char(\\(\\d+\\))?", "java.lang.String"));
        typeMapperList.add(new TypeMapper("text", "java.lang.String"));
        typeMapperList.add(new TypeMapper("decimal(\\(\\d+\\))?", "java.math.BigDecimal"));
        typeMapperList.add(new TypeMapper("decimal(\\(\\d+,\\d+\\))?", "java.math.BigDecimal"));
        typeMapperList.add(new TypeMapper("integer", "java.lang.Integer"));
        typeMapperList.add(new TypeMapper("int(\\(\\d+\\))?", "java.lang.Integer"));
        typeMapperList.add(new TypeMapper("int4", "java.lang.Integer"));
        typeMapperList.add(new TypeMapper("int8", "java.lang.Long"));
        typeMapperList.add(new TypeMapper("bigint(\\(\\d+\\))?", "java.lang.Long"));
        typeMapperList.add(new TypeMapper("datetime", "java.util.Date"));
        typeMapperList.add(new TypeMapper("timestamp", "java.util.Date"));
        typeMapperList.add(new TypeMapper("boolean", "java.lang.Boolean"));
        globalConfig.setTypeMapperList(typeMapperList);
    }

    public static GlobalConfig getGlobalConfig(String globalConfigFile) {
        //读取配置文件信息
        Properties properties = new Properties();
        // 使用ClassLoader加载properties配置文件生成对应的输入流
        InputStream in = Main.class.getClassLoader().getResourceAsStream(globalConfigFile);
        // 使用properties对象加载输入流
        try {
            properties.load(in);
        } catch (IOException e) {
            log.error("error:{}", e);
        }
        //获取key对应的value值
        JSONObject jsonObject = new JSONObject();
        Set<Object> objects = properties.keySet();
        for (Object obj : objects) {
            jsonObject.put(obj.toString(), properties.getProperty(obj.toString()));
        }
        GlobalConfig globalConfig = jsonObject.toJavaObject(GlobalConfig.class);
        return globalConfig;
    }

    public static DomainConfig getDomainConfig(String domainJsonFile) throws UnsupportedEncodingException {
        InputStreamReader in = new InputStreamReader(Main.class.getClassLoader().getResourceAsStream(domainJsonFile), "UTF-8");
        BufferedReader bufferedReader = new BufferedReader(in);
        StringBuilder stringBuilder = new StringBuilder();
        try {
            String lineTxt = null;
            while ((lineTxt = bufferedReader.readLine()) != null) {
                stringBuilder.append(lineTxt);
            }
        } catch (IOException e) {
            log.error(" error:{}", e);
            return null;
        }
        String dmianConfigStr = stringBuilder.toString();
        return JSONObject.parseObject(dmianConfigStr, DomainConfig.class);
    }

    /**
     * 获取默认参数
     *
     * @return 参数
     */
    public static Map<String, Object> getDefaultParam(GlobalConfig globalConfig) {
        Map<String, Object> param = new HashMap<>(20);
        // 作者
        param.put("author", globalConfig.getAuthor());
        //基础包名
        param.put("basePackage", globalConfig.getBasePackage());
        //工具类
        param.put("tool", GlobalTool.getInstance());
        param.put("time", TimeUtils.getInstance());
        return param;
    }
}
